package com.cd.works.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.cd.works.domain.Allservice;

/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Inside process(..) of Controller");
		Allservice service = new Allservice();
		int num = 0;
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		try {
		//Create a factory for disk-based file items
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);
		//parse the request
		List items = upload.parseRequest(request);
		System.out.println(items.toString());
		num = service.upload(items);
		if (num == 1) {
		out.println("<h3> File Uploaded Successfully </h3>");
		}else if (num == -1) {
		out.println("<h3><font color='red'> Please select the file</font></h3>");
		}
		else {
		out.println("<h3><font color='red'> Failure during File Upload </font></h3>");
		}
		} catch (Exception e) {
		}
		}
	

}
